package com.wy.system.controller;

import com.wy.common.result.Result;
import com.wy.model.entity.Permission;
import com.wy.model.vo.AssignMenuVo;
import com.wy.system.service.PermissionService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/system/permission")
public class PermissionController {
  @Autowired
  private PermissionService permissionService;

  //菜单列表(树形)
  @GetMapping("findNodes")
  public Result findNodes(){
    List<Permission> list = permissionService.findNodes();
    return Result.ok(list);
  }

  //添加菜单
  @PostMapping("save")
  public Result save(@RequestBody Permission permission){
    permissionService.save(permission);
    return Result.ok();
  }

  //根据id查询
  @PostMapping("findNode/{id}")
  public Result save(@PathVariable Integer id){
    Permission permission = permissionService.getById(id);
    return Result.ok(permission);
  }

  //修改菜单
  @PostMapping("update")
  public Result update(@RequestBody Permission permission){
    permissionService.updateById(permission);
    return Result.ok();
  }

  //删除菜单
  @DeleteMapping("remove/{id}")
  public Result remove(@PathVariable Integer id){
    permissionService.removePermissionById(id);
    return Result.ok();
  }

  //根据角色分配菜单
  @GetMapping("/toAssign/{roleId}")
  public Result toAssign(@PathVariable Integer roleId){
    List<Permission> list = permissionService.findMenuByRoleId(roleId);
    return Result.ok(list);
  }

  //给角色分配菜单权限
  @PostMapping("/doAssign")
  public Result doAssign(@RequestBody AssignMenuVo assignMenuVo){
    permissionService.doAssign(assignMenuVo);
    return Result.ok();
  }
}
